package com.ctshk.rpc.system.service.impl.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ctshk.common.constant.RedisConstants;
import com.ctshk.common.dto.Result;
import com.ctshk.rpc.system.dto.app.ScenicProductTypeDTO;
import com.ctshk.rpc.system.dto.app.ScenicProductTypeRedisDTO;
import com.ctshk.rpc.system.mapper.app.MainDataScenicProductTypeMapper;
import com.ctshk.rpc.system.service.app.IScenicProductTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Slf4j
@DubboService
public class ScenicProductTypeServiceImpl implements IScenicProductTypeService {
    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Autowired
    MainDataScenicProductTypeMapper mainDataScenicProductTypeMapper;

    @Override
    public Result<List<ScenicProductTypeRedisDTO>> selectScenicProductTypeList() {
        try{
            List<ScenicProductTypeRedisDTO> scenicProductTypeRedisDTOS = new ArrayList<>();
            // 从Redis中获取
            String result = redisTemplate.opsForValue().get(RedisConstants.SCENIC_PRODUCT_TYPE_LIST);
            if(result != null){
                scenicProductTypeRedisDTOS = JSON.parseArray(result, ScenicProductTypeRedisDTO.class);
            }else{
                List<ScenicProductTypeDTO> scenicProductTypeDTOS = mainDataScenicProductTypeMapper.selectScenicProductTypeList();
                if(scenicProductTypeDTOS == null){
                    scenicProductTypeDTOS = new ArrayList<>();
                }
                for (ScenicProductTypeDTO item : scenicProductTypeDTOS) {
                    ScenicProductTypeRedisDTO scenicProductTypeRedisDTO = new ScenicProductTypeRedisDTO();
                    scenicProductTypeRedisDTO.setTitle(item.getName());
                    if(item.getPhotos() != null){
                        try{
                            List<JSONObject> jsonObjects = JSON.parseArray(item.getPhotos(), JSONObject.class);
                            if(jsonObjects != null && jsonObjects.size() > 0){
                                JSONObject jsonObject = jsonObjects.get(0);
                                // 获取URL
                                String url = (String)jsonObject.get("url");
                                scenicProductTypeRedisDTO.setIcon(url);
                            }
                            scenicProductTypeRedisDTOS.add(scenicProductTypeRedisDTO);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
            // 写入 5-20分钟失效
            int time = 5 + new Random().nextInt(15);
            redisTemplate.opsForValue().set(RedisConstants.SCENIC_PRODUCT_TYPE_LIST, JSON.toJSONString(scenicProductTypeRedisDTOS), time, TimeUnit.MINUTES);
            return Result.success(scenicProductTypeRedisDTOS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.success(new ArrayList<>());
    }
}
